<?php
/**
 * Created by PhpStorm.
 * User: 跳梁
 * Date: 2016/9/29
 * Time: 14:48
 */
class ServicesAction extends Action{
    public function servicesMessageList($type=null,$division=null,$startTime=null,$endTime=null,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $cid=Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesMessage = M("services_message");
        Session::switchCustomer($cid);
        $where['a.cId']=array("eq",$cid);
        $where['a.deleted']=array("eq",0);
        $type?$where['a.typeId']=array("eq",$type):"";
        $division?$where['a.divisionId']=array("eq",$division):"";
        if($startTime&&!$endTime){
            $where['a.createTime']=array("gt",$startTime);
        }else if(!$startTime&&$endTime){
            $where['a.createTime']=array("lt",$endTime);
        }else if($startTime&&$endTime){
            $where['a.createTime']=array(array("lt",$endTime),array("gt",$startTime),'and');
        }
        $arr = $servicesMessage->alias("a")
            ->join("inner join wx_services_type b on a.typeId=b.id")
            ->join("left join wx_services_division c on a.divisionId=c.id")
            ->join("inner join wx_services_manager d on a.managerId=d.id")
            ->join("wx_".$cid."_user_weixin_info e on d.userId=e.id")
            ->where($where)
            ->field("a.id,a.allPerson,c.name as divisionName,b.name as typeName,e.nickname,e.headimgurl,a.createTime")
            ->limit($start,$limit)
            ->group("a.id")
            ->order("a.createTime desc")
            ->select();
        foreach($arr as $key=>$value){
            if($value['allPerson']){
                $arr[$key]['divisionName']='所有人';
            }
        }
        if($arr){
            $count = $servicesMessage->alias("a")
                ->join("inner join wx_services_type b on a.typeId=b.id")
                ->join("inner join wx_services_division c on a.divisionId=c.id")
                ->join("inner join wx_services_manager d on a.managerId=d.id")
                ->where($where)->count();
            Output::instance()->objArray($arr,$count);
        }else{
            Output::instance()->resultCode();
        }
    }
    //删除服务
    public function delMessageById($id){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $cid=Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesMessage = M("services_message");
        Session::switchCustomer($cid);
        $result = $servicesMessage->where("id=$id")->setField("deleted",1);
        if($result!==false){
            Output::instance()->resultCode();
        }else{
            Output::instance()->resultCode(code_sql_error);
        }

    }
    //服务详情
    public function servicesDetail($id){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesMessage  = M("services_message");
        Session::switchCustomer($customerId);
        $detail = $servicesMessage->alias("a")
            ->join("inner join wx_services_manager e on a.managerId=e.id")
            ->join("inner join wx_".$customerId."_user_weixin_info b on e.userId=b.id")
            ->join("inner join wx_services_type c on a.typeId=c.id")
            ->join("wx_services_division d on a.divisionId=d.id")
            ->where("a.id=$id")
            ->field("a.content,a.allPerson,a.image,b.nickname,b.headimgurl,c.name as typeName,a.createTime,d.name as divisionName")
            ->find();
        if($detail['image']){
            $detail['image']=explode(",",$detail['image']);
        }
        if($detail['allPerson']){
            $detail['divisionName']="所有人";
        }
        Output::instance()->object($detail);
    }
    //管理员列表
    public function managerList($nickname=null,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesManager = M("services_manager");
        Session::switchCustomer($customerId);
        $where['a.cId']=array("eq",$customerId);
        $nickname?$where['b.nickname']=array("like","%$nickname%"):"";
        $arr = $servicesManager->alias("a")
            ->join("inner join wx_".$customerId."_user_weixin_info b on a.userId=b.id")
            ->where($where)
            ->field("a.id,b.nickname,b.headimgurl,a.identity")
            ->limit($start,$limit)
            ->select();
        if($arr){
            $count =  $servicesManager->alias("a")
                ->join("inner join wx_".$customerId."_user_weixin_info b on a.userId=b.id")
                ->where($where)
                ->count();
            Output::instance()->objArray($arr,$count);
        }else{
            Output::instance()->resultCode();
        }

    }
    //删除管理员
    public function delManagerById($id){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesManager = M("services_manager");
        Session::switchCustomer($customerId);
        $result = $servicesManager->where("id=$id")->delete();
        if($result!==false){
            Output::instance()->resultCode();
        }else{
            Output::instance()->resultCode(code_sql_error);
        }

    }
    //设置或取消 管理员
    public function servicesIdentity($id,$identity){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesManager = M("services_manager");
        Session::switchCustomer($customerId);
        $result = $servicesManager->where("id=$id")->setField("identity",$identity);
        if($result!==false){
            Output::instance()->resultCode();
        }else{
            Output::instance()->resultCode(code_sql_error);
        }
    }
    //添加管理人员
    public function addManagerByIds($ids){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesManager = M("services_manager");
        Session::switchCustomer($customerId);
        $userInfo = M("user_weixin_info");
        $ids = explode(",",$ids);
        $data['cId']=$customerId;
        foreach($ids as $key=>$value){
            $openid = $userInfo->where("id=$value")->getField("openid");
            $data['userId']=$value;
            $data['openid']=$openid;
            $servicesManager->add($data);
        }
        Output::instance()->resultCode();

    }
    //服务类型列表
    public function servicesType($start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesType = M("services_type");
        Session::switchCustomer($customerId);
        $where['a.cId']=array("eq",$customerId);
        $arr = $servicesType->alias("a")
            ->join("wx_services_message b on a.id=b.typeId and b.deleted=0")
            ->where($where)
            ->field("a.id,a.name,count(b.id) as count")
            ->limit($start,$limit)
            ->group("a.id")
            ->select();
        if($arr){
            $count = $servicesType->alias("a")->where($where)->count();
            Output::instance()->objArray($arr,$count);
        }else{
            Output::instance()->resultCode();
        }
    }
    //删除服务类型
    public function delServicesType($id){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesType = M("services_type");
        Session::switchCustomer($customerId);
        $result = $servicesType->where("id=$id")->delete();
        if($result!==false){
            Output::instance()->resultCode();
        }else{
            Output::instance()->resultCode(code_sql_error);
        }
    }
    //添加服务
    public function addServicesType($name){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesType = M("services_type");
        Session::switchCustomer($customerId);
        $data['name']=$name;
        $data['cId']=$customerId;
        $result = $servicesType->add($data);
        if($result){
            Output::instance()->resultCode();
        }else{
            Output::instance()->resultCode(code_sql_error);
        }
    }
    //机构列表
    public function servicesDivision($name=null,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesDivision = M("services_division");
        Session::switchCustomer($customerId);
        $where['a.cId']=array("eq",$customerId);
        $name?$where['a.name']=array("like","%$name%"):"";
        $arr = $servicesDivision->alias("a")
            ->join("wx_services_division_user b on a.id=b.divisionId")
            ->where($where)
            ->field("a.id,a.name,count(b.id) as count,createTime")
            ->group("a.id")
            ->limit($start,$limit)
            ->order("a.updateTime desc")
            ->select();
        if($arr){
            $count = $servicesDivision->alias("a")->where($where)->count();
            Output::instance()->objArray($arr,$count);
        }else{
            Output::instance()->resultCode();
        }
    }
    //删除机构
    public function delServicesDivision($id){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesDivision = M("services_division");
        $servicesDivisionUser = M("services_division_user");
        Session::switchCustomer($customerId);
        $servicesDivision->startTrans();
        $result = $servicesDivision->where("id=$id")->delete();
        $userResult = $servicesDivisionUser->where("divisionId=$id")->delete();
        if($result!==false&&$userResult!==false){
            $servicesDivision->commit();
            Output::instance()->resultCode();
        }else{
            $servicesDivision->rollback();
            Output::instance()->resultCode(code_sql_error);
        }
    }
    //机构下的人
    public function getServicesUser($id,$nickname=null,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesDivisionUser = M("services_division_user");
        Session::switchCustomer($customerId);
        $where['a.divisionId']=array("eq",$id);
        $nickname?$where['b.nickname']=array("like","%$nickname%"):"";
        $arr = $servicesDivisionUser->alias("a")
            ->join("wx_".$customerId."_user_weixin_info b on a.userId=b.id")
            ->where($where)
            ->field("a.id,b.nickname,b.headimgurl")
            ->limit($start,$limit)
            ->select();
        if($arr){
           $count = $servicesDivisionUser->alias("a")
                ->join("wx_".$customerId."_user_weixin_info b on a.userId=b.id")
                ->where($where)
                ->count();
            Output::instance()->objArray($arr,$count);
        }else{
            Output::instance()->resultCode();
        }
    }
    //删除服务对象
    public function delDivisionUser($id){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesDivisionUser = M("services_division_user");
        Session::switchCustomer($customerId);
        $result = $servicesDivisionUser->where("id=$id")->delete();
        if($result!==false){
            Output::instance()->resultCode();
        }else{
            Output::instance()->resultCode(code_sql_error);
        }

    }
    //添加服务对象
    public function addDivisionUserByIds($ids,$divisionId){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesDivisionUser = M("services_division_user");
        Session::switchCustomer($customerId);
        $userInfo = M("user_weixin_info");
        $ids = explode(",",$ids);
        $data['divisionId']=$divisionId;
        foreach($ids as $key=>$value){
            $openid = $userInfo->where("id=$value")->getField("openid");
            $data['userId']=$value;
            $data['openid']=$openid;
            $servicesDivisionUser->add($data);
        }
        Output::instance()->resultCode();

    }
    //获得机构或服务类型信息
    public function getDivisionOrTypeInfo($id,$type){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesDivision = M("services_division");
        $servicesType = M("services_type");
        Session::switchCustomer($customerId);
        if($type==1){
            $result = $servicesDivision->where("id=$id")->find();
        }else if($type==2){
            $result = $servicesType->where("id=$id")->find();
        }
        Output::instance()->object($result);
    }
    //修改机构或服务类型信息
    public function setDivisionOrTypeInfo($id=null,$type,$name){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesDivision = M("services_division");
        $servicesType = M("services_type");
        Session::switchCustomer($customerId);
        $data['name']=$name;
        if($type==1){
            if($id){
                $data['updateTime']=date("Y-m-d H:i:s");
                $result = $servicesDivision->where("id=$id")->save($data);
            }else{
                $data['cId']=$customerId;
                $data['createTime']=date("Y-m-d H:i:s");
                $data['updateTime']=date("Y-m-d H:i:s");
                $result = $servicesDivision->add($data);
            }

        }else if($type==2){
            $result = $servicesType->where("id=$id")->save($data);
        }
        if($result!==false){
            Output::instance()->resultCode();
        }else{
            Output::instance()->resultCode(code_sql_error);
        }
    }
    //服务对象统计
    public function divisionStatistics($startTime=null,$endTime=null,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesDivision = M("services_division");
        Session::switchCustomer($customerId);
        $where['a.cId']=array("eq",$customerId);
        $str='';
        if($startTime){
            $str.="and b.createTime>'$startTime'";
        }
        if($endTime){
            $str.="and b.createTime<'$endTime'";
        }
        $arr = $servicesDivision->alias("a")
            ->join("wx_services_message b on a.id=b.divisionId and b.deleted=0 ".$str)
            ->where($where)
            ->field("a.id,a.name,count(b.id) as count")
            ->limit($start,$limit)
            ->group("a.id")
            ->select();
        if($arr){
            $count = $servicesDivision->where("cId=$customerId")->count();
            Output::instance()->objArray($arr,$count);
        }else{
            Output::instance()->resultCode();
        }
    }
    //服务类型
    public function typeStatistics($startTime=null,$endTime=null,$start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $servicesType = M("services_type");
        Session::switchCustomer($customerId);
        $where['a.cId']=array("eq",$customerId);
        $str='';
        if($startTime){
            $str.="and b.createTime>'$startTime'";
        }
        if($endTime){
            $str.="and b.createTime<'$endTime'";
        }
        $arr = $servicesType->alias("a")
            ->join("wx_services_message b on a.id=b.typeId and b.deleted=0 ".$str)
            ->where($where)
            ->field("a.id,a.name,count(b.id) as count")
            ->limit($start,$limit)
            ->group("a.id")
            ->select();
        if($arr){
            $count = $servicesType->where("cId=$customerId")->count();
            Output::instance()->objArray($arr,$count);
        }else{
            Output::instance()->resultCode();
        }
    }
}